﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Input;
using AttachedCommandBehavior;
using MathematicallySafe.HeadCounter.Commands;
using MathematicallySafe.HeadCounter.Common;
using MathematicallySafe.HeadCounter.Data;
using MathematicallySafe.HeadCounter.Data.Models;
using MathematicallySafe.HeadCounter.Services;

namespace MathematicallySafe.HeadCounter.ViewModels
{
    public class RaidMemberViewModel: RaidMemberViewModelBase
    {
        private int _lootWonCount;
        private int _minutes;

        private DelegateCommand _dropCommand;

        public RaidMemberViewModel(string raidingName) : base(raidingName)
        {
        }

        public int Minutes
        {
            get { return _minutes; }
            set
            {
                _minutes = value;
                OnPropertyChanged("Minutes");
            }
        }

        public int ItemsWonCount
        {
            get { return _lootWonCount; }
            set
            {
                _lootWonCount = value;
                OnPropertyChanged("ItemsWonCount");
            }
        }

        public ICommand DropCommand
        {
            get
            {
                if (_dropCommand == null)
                {
                    _dropCommand = new DelegateCommand(Drop);
                }

                return _dropCommand;
            }
        }

        private void Drop()
        {
            // todo dont like this much
            var droppedLoot = DragDropManager.DraggedData as RaidLootViewModel;

            if (droppedLoot != null)
            {
                droppedLoot.Outcome = LootOutcome.Won;
                droppedLoot.Winner = Name;
            }
        }
    }

    public class NonRaidMemberViewModel : RaidMemberViewModelBase
    {
        private Raid _raid;

        public NonRaidMemberViewModel(Raid raid, string identity)
            : base(identity)
        {
            _raid = raid;
        }

        public bool Attended
        {
            get { return _raid.IsExtraAttendee(Name); }
            set
            {
                var attended = value;

                if (attended)
                {
                    if (!_raid.IsExtraAttendee(Name)) _raid.AddExtraAttendee(Name);
                }
                else
                {
                    if (_raid.IsExtraAttendee(Name)) _raid.RemoveExtraAttendee(Name);
                }

                OnPropertyChanged("Attended");
            }
        }
    }
}
